package com.buxiaoxia.business.entity;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;


import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;


@Data

public class User implements UserDetails {


	private static final long serialVersionUID = 7560556433731496932L;
	private int id;

	private String name;

	private String username;

	private String password;

	private Date createAt;

	private List<SysRole>  roles;



	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		// 根据自定义逻辑来返回用户权限，如果用户权限返回空或者和拦截路径对应权限不同，验证不通过
		List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
		if(!roles.isEmpty()){
			for(SysRole s:roles) {
				GrantedAuthority au = new SimpleGrantedAuthority(s.getName());
				list.add(au);
			}
		}
		return list;


	}
	@Override
	public String getUsername() {
		return this.username;
	}

	@Override
	public String getPassword() {
		return this.password;
	}


	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}
}
